#include "../includes.h"
#include "udp_includes.h"




int squence_num=0;
int msg_pack_header(IPC_Data_T *p,int seq)
{
	//IPC_Data_T *p;
	//p = &ud->msg.out;
	//	p->ipc.nickname = M_KTI_NICK_NAME;
	//	p->ipc.version[0]=M_KTI_NMS_VERSION;
	//	p->ipc.version[1]=M_KTI_RCU_VERSION;
//	strncpy(p->ipc.master_id , GetMasterID(),10 );
//	strncpy(p->ipc.equip_no ,  GetEquipNum(),8 );
	p->ipc = buf_ipc;
	strncpy(p->ipc.start ,"$KTF",4 );
	if( seq == -1 ) p->ipc.sequence =  squence_num++;
	else p->ipc.sequence =  seq;
	//p->ipc.sequence =0x1234;
	return 0;
}

int8u *pCrc;
int16u vCrc;

int msg_swap_and_send(IPC_Data_T *pp)
{
	//swap byte in header
	int16u len2;
	int16u crc1,crc2;
	//calc crc
	//*pCrc = htons(vCrc);
//	pCrc[1]=(vCrc>>8)&&0xFF;
//	pCrc[0]=vCrc&&0xFF;
	//udp_printf("\ncrc>>0x%04X==>0x%2X%2X-->0x%2X%2X\n",vCrc,(vCrc>>8)&0xFF,vCrc&0xFF,pCrc[0]&0xFF,pCrc[1]&0xFF);


	int len= IPC_KTI_HEADER_LENGTH+pp->ipc.len+IPC_SIZEOF_KTI_TAIL;

	pp->ipc.sequence = htons(pp->ipc.sequence);

	len2 = pp->ipc.len;
	pp->ipc.len = htons(len2);

//	crc1 = GenerateCRCShort( (int8u *)&pp->ipc.command, IPC_KTI_HEADER_LENGTH - IPC_OFFSET_OF_CRC_START + pp->ipc.len  );

	//swap byte in user data
	//udp_MemoryHexDump((char*)pp, len);
	msg_hton_data(pp->buf, (int8)pp->ipc.command);
	//udp_printf("--after(s)---\n");
	//udp_MemoryHexDump((char*)pp, len);
//	crc2 = GenerateCRCShort( (int8u *)&pp->ipc.command, IPC_KTI_HEADER_LENGTH - IPC_OFFSET_OF_CRC_START + pp->ipc.len  );
//	udp_printf(">>tempCrc:0x%04X 0x%04X\n",crc1&0xFFFF ,crc2&0xFFFF);

	strncpy((char*)((int)pp->buf+len2+2),"$X",2);

	pCrc =(int8u *)( (int)(&pp->ipc.len)+sizeof(pp->ipc.len)+len2 );
	vCrc = GenerateCRCShort( (int8u *)&pp->ipc.command
			, IPC_KTI_HEADER_LENGTH - IPC_OFFSET_OF_CRC_START + len2  );
	pCrc[0]=(vCrc>>8)&0xFF;
	pCrc[1]=vCrc&0xFF;

//	net_send((char*)pp, len,0);  //kay
	return 0;
}



//////////////////////////////////////////////
//////////////////////////////////////////////
int msg_pack_data_heartbeat_rsp(UDP_DAEMON_* ud,int seq)
{
	IPC_Data_T *pp;
	KTI_SubData_HeartbeatRsp_T* p;
	pp = (IPC_Data_T *)&ud->msg.out;
	p = (KTI_SubData_HeartbeatRsp_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_HEARTBEAT_RSP;

	pp->ipc.len = sizeof(KTI_SubData_HeartbeatRsp_T);

	//msg_set_data_report(p);
	p->period = CONFIG.iPeriodHeartBeat;

	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_data_heartbeat_rsp:%d\r\n",CONFIG.iPeriodHeartBeat);
	return 0;
}

char hearbestdata[8] ={ 0x00 ,0x00 ,0x00 ,0x00    ,0x00 ,0x00 ,0x00 ,0x00};
int msg_pack_data_heartbeat()
{

	net_send( hearbestdata, 8, 0);

	udp_printf(">>>>> issue msg_pack_data_heartbeat:%d\r\n",CONFIG.iPeriodHeartBeat);
	return 0;
}

//////////////////////////////////////////////

int msg_pack_data_report(UDP_DAEMON_* ud,int seq)
{
	msg_pack_data_report2nd(ud ,seq, 0);
	return 0;
}

int msg_pack_data_report2nd(UDP_DAEMON_* ud,int seq,int flag)
{
	IPC_Data_T *pp;
	KTI_SubData_Report_T* p;
	pp = (IPC_Data_T *)&ud->msg.out;
	p = (KTI_SubData_Report_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_STATUS_REPORT;

	pp->ipc.len = sizeof(KTI_SubData_Report_T);

	//msg_set_data_report(p);
	*p = buf_report;

	p->flag = flag ;//GetReportFlag();

	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_data_report\r\n");
	return 0;
}
int msg_pack_status_req(UDP_DAEMON_* ud,int seq)
{
	IPC_Data_T *pp;
	//KTI_SubData_StatusReq_T* p;

	pp = (IPC_Data_T *)&ud->msg.out;
	//p = (KTI_SubData_StatusReq_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_STATUS_REQ;//KTC_STATUS_REQ;
	pp->ipc.len = 0;//sizeof(KTI_SubData_StatusReq_T);

	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_status_req\r\n");
	return 0;
}


int msg_pack_data_alarm(UDP_DAEMON_* ud,int seq)
{
	IPC_Data_T *pp;
	KTI_SubData_Alarm_T* p;

	pp = (IPC_Data_T *)&ud->msg.out;
	p = (KTI_SubData_Alarm_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_ALARM_REPORT;
	pp->ipc.len = sizeof(KTI_SubData_Alarm_T);

	msg_set_data_alarm(p);
	//p->alarm = buf_alm;
	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_data_alarm,v:%02X, evnt:%02X,\r\n"
			,buf_report.alarm.value.c, buf_report.alarm.event.c);
	return 0;
}


int msg_pack_control_req(UDP_DAEMON_* ud,int seq)
{
	IPC_Data_T *pp;
	KTI_SubData_CtrlReq_T* p;

	pp = (IPC_Data_T *)&ud->msg.out;
	p = (KTI_SubData_CtrlReq_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_CONTRL_REQ;
	pp->ipc.len = sizeof(KTI_SubData_CtrlReq_T);

	msg_set_control_req(p);
	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_control_req\r\n");
	return 0;
}


int msg_pack_ip_req(UDP_DAEMON_* ud,int seq)
{
	IPC_Data_T *pp;
	KTI_SubData_IpReq_T* p;

	pp = (IPC_Data_T *)&ud->msg.out;
	p = (KTI_SubData_IpReq_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_IP_CHANGE_REQ;
	pp->ipc.len = sizeof(KTI_SubData_IpReq_T);

	msg_set_ip_req(p);
	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_ip_req\r\n");
	return 0;
}
int msg_pack_ip_rsp(UDP_DAEMON_* ud,int seq)
{
	IPC_Data_T *pp;
	//KTI_SubData_ResetReq_T* p;

	pp = (IPC_Data_T *)&ud->msg.out;
	//p = (KTI_SubData_ResetReq_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_IP_CHANGE_RSP;
	pp->ipc.len = 0;//sizeof(KTI_SubData_ResetReq_T);

	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_ip_rsp\r\n");
	return 0;
}


int msg_pack_reset_req(UDP_DAEMON_* ud,int seq)
{
	IPC_Data_T *pp;

	pp = (IPC_Data_T *)&ud->msg.out;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_RESET_REQ;
	pp->ipc.len = 0;//sizeof(KTI_SubData_ResetReq_T);

	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_reset_rsp\r\n");
	return 0;
}
int msg_pack_reset_rsp(UDP_DAEMON_* ud,int seq)
{
	IPC_Data_T *pp;

	pp = (IPC_Data_T *)&ud->msg.out;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_RESET_RSP;
	pp->ipc.len = 0;

	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_reset_rsp\r\n");
	return 0;
}


int msg_pack_mac_req(UDP_DAEMON_* ud,int seq)
{
	IPC_Data_T *pp;
	//KTI_SubData_StatusReq_T* p;

	pp = (IPC_Data_T *)&ud->msg.out;
	//p = (KTI_SubData_StatusReq_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_MAC_ADDR_REQ;//KTC_STATUS_REQ;
	pp->ipc.len = 0;//sizeof(KTI_SubData_StatusReq_T);

	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_mac_rsp\r\n");
	return 0;
}
int msg_pack_mac_rsp(UDP_DAEMON_* ud,int seq)
{
	IPC_Data_T *pp;
	KTI_SubData_MacRsp_T* p;

	pp = (IPC_Data_T *)&ud->msg.out;
	p = (KTI_SubData_MacRsp_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_MAC_ADDR_RSP;//KTC_STATUS_REQ;
	pp->ipc.len = sizeof(KTI_SubData_MacRsp_T);

	msg_set_mac_rsp(p);
	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_mac_rsp\r\n");
	return 0;
}

/////// Down load
int msg_pack_download_req(UDP_DAEMON_* ud,int seq)
{
	IPC_Data_T *pp;
	KTI_SubData_DL_CmdReq_T* p;

	pp = (IPC_Data_T *)&ud->msg.out;
	p = (KTI_SubData_DL_CmdReq_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_DL_CMD_REQ;
	pp->ipc.len = sizeof(KTI_SubData_DL_CmdReq_T);

	msg_set_download_req(p);

	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_download_req\r\n");
	return 0;
}
int msg_pack_download_rsp(UDP_DAEMON_* ud,int seq,KTI_SubData_DL_CmdRsp_T *ptr)
{
	IPC_Data_T *pp;
	KTI_SubData_DL_CmdRsp_T* p;

	pp = (IPC_Data_T *)&ud->msg.out;
	p = (KTI_SubData_DL_CmdRsp_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_DL_CMD_RSP;
	pp->ipc.len = sizeof(KTI_SubData_DL_CmdRsp_T);

	//msg_set_download_rsp(p);
	memcpy(p,ptr,sizeof(KTI_SubData_DL_CmdRsp_T));

	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_download_rsp\r\n");
	return 0;
}


int msg_pack_download_data(UDP_DAEMON_* ud,int seq,char *buf,int size)
{
	IPC_Data_T *pp;
	KTI_SubData_DL_Data_T* p;

	pp = (IPC_Data_T *)&ud->msg.out;
	p = (KTI_SubData_DL_Data_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_DL_DATA;

	pp->ipc.len = sizeof(KTI_SubData_DL_Data_T)+size;

	p->total_frame 		= GetDownloadTotalFrame();
	p->current_frame 	= GetDownloadCurrentFrame();

	//memcpy((void*)((int)&p->current_frame+sizeof(p->current_frame)),GetDownloadCurrentPointer(),GetDownloadCurrentSize());
	memcpy((void*)((int)&p->current_frame+sizeof(p->current_frame)),buf,size);

	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_download_data\r\n");
	return 0;
}
int msg_pack_download_data_rsp(UDP_DAEMON_* ud,int seq,KTI_SubData_DL_DataRsp_T *ptr)
{
	IPC_Data_T *pp;
	KTI_SubData_DL_DataRsp_T* p;

	pp = (IPC_Data_T *)&ud->msg.out;
	p = (KTI_SubData_DL_DataRsp_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_DL_DATA_RSP;

	pp->ipc.len = sizeof(KTI_SubData_DL_DataRsp_T);

//	p->total_frame = 0x10FF;
//	p->current_frame = 0xFF01;
	memcpy(p,ptr,sizeof(KTI_SubData_DL_DataRsp_T));

	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_download_data\r\n");
	return 0;
}



int msg_pack_download_complete_req(UDP_DAEMON_* ud,int seq)
{
	IPC_Data_T *pp;
	KTI_SubData_DL_CompleteReq_T* p;

	pp = (IPC_Data_T *)&ud->msg.out;
	p = (KTI_SubData_DL_CompleteReq_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_DL_COMPLETE_REQ;
	pp->ipc.len = sizeof(KTI_SubData_DL_CompleteReq_T);

	msg_set_download_complete_req(p);

	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_download_complete_req\r\n");
	return 0;
}
int msg_pack_download_complete_rsp(UDP_DAEMON_* ud,int seq,int16u crc,int8 rlt)
{
	IPC_Data_T *pp;
	KTI_SubData_DL_CompleteRsp_T* p;

	pp = (IPC_Data_T *)&ud->msg.out;
	p = (KTI_SubData_DL_CompleteRsp_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_DL_COMPLETE_RSP;
	pp->ipc.len = sizeof(KTI_SubData_DL_CompleteRsp_T);

	msg_set_download_complete_rsp(p,crc,rlt);

	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_download_complete_rsp: fw_file_crc:0x%04X\r\n",crc);
	return 0;
}

// Local Cmd
int msg_pack_CmdReq(UDP_DAEMON_* ud,int seq,char *buf,int size)
{
	IPC_Data_T *pp;
	//KTI_SubData_DL_Data_T* p;

	pp = (IPC_Data_T *)&ud->msg.out;
	//p = (KTI_SubData_DL_Data_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_LOCAL_REQ;

	pp->ipc.len = size;

	memcpy((void*)((int)&pp->ipc.len +sizeof(pp->ipc.len)),buf,size);
	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_CmdReq\r\n");
	return 0;
}
int msg_pack_CmdRsp(UDP_DAEMON_* ud,int seq,char *buf,int size)
{
	IPC_Data_T *pp;
	//KTI_SubData_DL_Data_T* p;

	pp = (IPC_Data_T *)&ud->msg.out;
	//p = (KTI_SubData_DL_Data_T*)&ud->msg.out.buf;

	//make header
	msg_pack_header (pp,seq); // seq -1:auto Increase
	pp->ipc.command = KTC_LOCAL_RSP;

	pp->ipc.len = size;

	memcpy((void*)((int)&pp->ipc.len +sizeof(pp->ipc.len)),buf,size);
	msg_swap_and_send(pp);

	udp_printf(">>>Send(0x%02X): %d,%d,%d,0x%04X",pp->ipc.command,IPC_KTI_HEADER_LENGTH , IPC_OFFSET_OF_CRC_START , ntohs(pp->ipc.len),vCrc);
	udp_printf(">>>>> issue msg_pack_CmdRsp\r\n");
	return 0;
}

void udp_local_cmd_send(char* p)
{
	//printf("--%d--\n",strlen(p));
	if( lcd_data.cnt_idle>0){
		//lcd_data.cnt_idle = 1;
		msg_pack_CmdRsp(&udp,-1,p,strlen(p));
	}
}

void udp_local_cmd_send_size(char* p,int len)
{
	//printf("--%d--\n",strlen(p));
	if( lcd_data.cnt_idle>0){
		//lcd_data.cnt_idle = 1;
		msg_pack_CmdRsp(&udp,-1,p,len);
	}
}

///////////////////////////////////////////////////
///////////////////////////////////////////////////
///////////////////////////////////////////////////
///////////////////////////////////////////////////


void make_rcu_alarm(KTI_SubData_Alm_T *p)
{

	//AssignAlarm();
	*p = buf_alm_saved;

//	p->event.b.ant_open = 0;
//	p->value.b.ant_open = 0;
//
//	p->event.b.ant_short = 0;
//	p->value.b.ant_short = 0;
//
//	p->event.b.clk_na = 1;
//	p->value.b.clk_na = 1;
//
//	p->event.b.gps_holdover = 0;
//	p->value.b.gps_holdover = 0;
//
//	p->event.b.ocxo_efc = 0;
//	p->value.b.ocxo_efc = 0;
//
//	p->event.b.ocxo_fault = 0;
//	p->value.b.ocxo_fault = 0;

	p->event.b.rsvd1 = 0;
	p->value.b.rsvd1 = 0;

	p->event.b.rsvd2 = 0;
	p->value.b.rsvd2 = 0;

	p->rsvd1 = 0x8080;
	p->rsvd2 = 0x8080;
	
	udp_printf("alm: evnt:0x%02X value:0x%02X\n",p->event.c, p->value.c );

}

int msg_set_data_alarm(KTI_SubData_Alarm_T* p)
{
	//make user data
	make_rcu_alarm(&p->alarm);
	return 0;
}


int msg_set_control_req(KTI_SubData_CtrlReq_T* p)
{
	p->event = 1;
	p->period = 0x10;
	memset(p->rsvd1,0x80,10);
	memset(p->rsvd2,0x80,10);
	return 0;

}

int msg_set_ip_req(KTI_SubData_IpReq_T* p)
{
	p->ip[0]=0x10;
	p->ip[1]=0x20;
	p->ip[2]=0x30;
	p->ip[3]=0x40;

	p->port = 0x4000;
	p->rsvd = 0x8080;
	return 0;
}

int msg_set_download_req(KTI_SubData_DL_CmdReq_T* p)
{
	p->version[0]=0x10;
	p->version[1]=0x20;
	p->supplier1 = 0x50;
	p->supplier2 = 0x50;
	p->total_frame =GetDownloadTotalFrame();
	p->rsvd = 0x8080;
	return 0;
}

//int msg_set_download_rsp(KTI_SubData_DL_CmdRsp_T* p)
//{
//	p->version[0]=0x10;
//	p->version[1]=0x20;
//	p->supplier1 = 0x50;
//	p->supplier2 = 0x50;
//	p->total_frame =0x100;
//	p->ready = 1;
//	p->rsvd  = 0x80;
//
//}

int msg_set_download_complete_req(KTI_SubData_DL_CompleteReq_T* p)
{
	p->version[0]	= 0x10;
	p->version[1]	= 0x20;
	p->supplier1 	= 0x50;
	p->supplier2 	= 0x50;
	p->total_frame 	= 0x100;
	p->total_crc 	= GetTotalCrc();
	return 0;
}

extern int file_size_downloaded;
int msg_set_download_complete_rsp(KTI_SubData_DL_CompleteRsp_T* p,int16u crc, int8 rlt)
{
	void *m;
	int fdFile;

	p->supplier1 = 0x50;
	p->supplier2 = 0x50;

	//p->total_crc = 0x100;
	//M_DL_FILE_NAME_WR
	if( rlt ){
		p->total_crc = crc;
		p->result 	= 0x01;
	}
	else{
		p->total_crc = 0x0000;
		p->result 	= 0x00;
	}
	p->rsvd[0] 	= 0x80;
	p->rsvd[1] 	= 0x80;
	p->rsvd[2] 	= 0x80;

	udp_printf("received crc:0x%04X",crc);
	return 0;
}


int msg_set_mac_rsp(KTI_SubData_MacRsp_T* p)
{
	int8u mac[]={0xFF,0x55,0xFF,0x55, 0xFF,0x55 };
	memcpy(p->mac,mac,6);
	GetIPconfig(ip_cfg_MAC ,p->mac);
	p->flag = 0;
	p->rsvd = 0x8080;
	return 0;
}

